/*
 * @Author: huangxuzhong
 * @Date: 2021-11-17 14:09:34
 * @LastEditTime: 2021-11-17 14:41:04
 * @LastEditors: Please set LastEditors
 * @Description: 隧道工具类
 * @FilePath: \macc.admin.vue\src\components\rj-tunnel\tunnel-tools.js
 */
import { maccApi, requestHttp } from '@utils/index';
const ProductClassRule = 'RAC'; //过滤设备类型
const TunnelNonSupportVersion = ['11.1(5)B23', '11.1(5)B27']; //不支持隧道版本号
import { i18nRender } from '@/locales';

const TunnelUtil = {
  //校验隧道版本
  tunnelCheckVersion(version, prodeuctClass) {
    for (let i = 0; i < TunnelNonSupportVersion.length; i++) {
      if (
        version.indexOf(TunnelNonSupportVersion[i]) > 0 &&
        prodeuctClass.indexOf(ProductClassRule) == 0
      ) {
        return false;
      }
    }
    return true;
  },
  async getDeviceTunnelAbility(sn) {
    const res = await this.getDeviceTunnelAbilityList(sn);
    if (res.code == 0 && res.tunnelEnable) {
      return this.formatterTunnelAbility(res.list || []);
    } else {
      return {};
    }
  },
  //获取设备隧道能力值
  async getDeviceTunnelAbilityList(sn) {
    const res = await requestHttp({
      api: maccApi.GET_TUNNEL_ABILITY,
      method: 'POST',
      params: { sn: sn },
    });
    return res;
  },
  //格式化隧道能力值
  formatterTunnelAbility(abilityList) {
    const abilitys = {};
    abilityList.forEach((item) => {
      if (item.type == 'SSH' && item.status == 'enabled') {
        abilitys[item.type] = item.port;
      } else if (item.type == 'TELNET' && item.status == 'enabled') {
        abilitys[item.type] = item.port;
      } else if (item.type == 'HTTP' && item.status == 'enabled') {
        abilitys[item.type] = item.port;
      } else if (item.type == 'HTTPS' && item.status == 'enabled') {
        abilitys[item.type] = item.port;
      }
    });
    return abilitys;
  },
  //获取关闭得隧道类型
  getDisabledTunnelType(abilityList) {
    const type = [];
    abilityList.forEach((item) => {
      if (item.type == 'SSH' && item.status == 'disabled') {
        type.push(item.type);
      } else if (item.type == 'TELNET' && item.status == 'disabled') {
        type.push(item.type);
      } else if (item.type == 'HTTP' && item.status == 'disabled') {
        type.push(item.type);
      } else if (item.type == 'HTTPS' && item.status == 'disabled') {
        type.push(item.type);
      }
    });
    return type;
  },
  //获取不支持得隧道类型
  getNonsupportTunnelType(abilityList) {
    const type = [];
    abilityList.forEach((item) => {
      if (item.type == 'SSH' && item.status == 'nonsupport') {
        type.push(item.type);
      } else if (item.type == 'TELNET' && item.status == 'nonsupport') {
        type.push(item.type);
      } else if (item.type == 'HTTP' && item.status == 'nonsupport') {
        type.push(item.type);
      } else if (item.type == 'HTTPS' && item.status == 'nonsupport') {
        type.push(item.type);
      }
    });
    return type;
  },
  /**
   * 获取网络中可以作为隧道主设备的网络设备
   */
  async getNetworkSupportTunnelDevice(params) {
    const res = await requestHttp({
      api: `${maccApi.TUNNEL_GETTUNNELDEVICELIST_POST_API}`,
      method: 'POST',
      params: params,
    });
    if (res && res.code == 0) {
      return res.list || [];
    } else {
      return [];
    }
  },
  /**
   * 获取网络中可以作为目标设备的网络设备
   */
  async getNetworkTargetTunnelDevice(params) {
    const res = await requestHttp({
      api: `${maccApi.TUNNEL_CREATDTARGETDEVICETUNNEL_POST_API}`,
      method: 'POST',
      params: params,
    });
    if (res && res.code == 0) {
      const list = res.list || [];
      return [
        {
          localIp: '127.0.0.1',
          mask: 8,
          name: i18nRender('deviceDetail.local'),
          sn: params.sn,
        },
        ...list,
      ];
    } else {
      return [];
    }
  },
  /**
   * 创建隧道
   */
  async creatTunnel(params) {
    const api = params.operation_source
      ? `${maccApi.TUNNEL_CREATDEVICETUNNEL_POST_API}&operation_source=${params.operation_source}`
      : maccApi.TUNNEL_CREATDEVICETUNNEL_POST_API;
    if (params.operation_source) {
      delete params.operation_source;
    }
    const res = await requestHttp({
      api,
      method: 'POST',
      params,
    });
    return res;
  },
  /**
   * 关闭隧道
   * @param {*} params
   * @returns
   */
  async destroyTunnel(params) {
    const api = maccApi.TUNNELDESTROY_POST_API;
    const res = await requestHttp({
      api,
      method: 'POST',
      params,
    });
    return res;
  },
  /**
   * 获取管理密码状态
   * @returns
   */
  async getEnetPwdStatus(groupId) {
    const res = await requestHttp({
      api: `${maccApi.ENTER_PASSWORDSTATUS_GET_API.replace(
        '{group_id}',
        groupId
      )}`,
      method: 'GET',
    });
    return res;
  },
};

export { TunnelUtil };
